package com.pb.controller;

import com.pb.pojo.Subject;
import com.pb.pojo.PageResult;
import com.pb.pojo.Result;
import com.pb.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/subjects")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    @PostMapping
    public Result save(@RequestBody Subject subject){
        subjectService.save(subject);
        return Result.success();
    }

    @GetMapping
    public Result page(String name ,
                       @RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult pageResult = subjectService.page(name, page , pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        Subject subject = subjectService.getInfo(id);
        return Result.success(subject);
    }


    @PutMapping
    public Result update(@RequestBody Subject subject){
        subjectService.update(subject);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        subjectService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Subject> subjectList = subjectService.findAll();
        return Result.success(subjectList);
    }
}
